package com.itfreer.ui.photoview;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.itfreer.core.config.ConfigManager;
import com.itfreer.ui.R;

import java.io.File;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * 单个图片查看器
 */
public class PhotoFragment extends Fragment {

    private String url;
    private PhotoView mPhotoView;

    /**
     * 获取这个fragment需要展示图片的url
     * @param url
     * @return
     */
    public static PhotoFragment newInstance(String url) {
        PhotoFragment fragment = new PhotoFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getArguments().getString("url");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_img, container, false);
        mPhotoView = view.findViewById(R.id.photoview);

        ImageView photovideo = view.findViewById(R.id.photovideo);
        String[] files = url.split("\\.");
        String objectKey = files[files.length-1];
        objectKey = objectKey.toLowerCase();
        if(objectKey.equals("jpg") || objectKey.equals("png") || objectKey.equals("jpeg") || objectKey.equals("gif")
                || objectKey.equals("bpm") || objectKey.equals("webp")) {

        } else if(objectKey.equals("mp3") || objectKey.equals("mp4") || objectKey.equals("3gp") || objectKey.equals("webm")) {
            photovideo.setVisibility(View.VISIBLE);
            mPhotoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    Intent intent = new Intent();
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    File file = new File(url);
                    Uri uri;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Uri contentUri = FileProvider.getUriForFile(ConfigManager.getContext(), ConfigManager.getContext().getPackageName() + ".provider", file);
                        intent.setDataAndType(contentUri, "video/*");
                    } else {
                        uri = Uri.fromFile(file);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setDataAndType(uri, "video/*");
                    }
                    startActivity(intent);
                }
            });
        }

        //设置缩放类型，默认ScaleType.CENTER（可以不设置）
        mPhotoView.setScaleType(ImageView.ScaleType.CENTER);
        Glide.with(getContext())
            .load(url)
            .placeholder(R.mipmap.timg)//加载过程中图片未显示时显示的本地图片
            .error(R.mipmap.timg)//加载异常时显示的图片
//                .centerCrop()//图片图填充ImageView设置的大小
            .fitCenter()//缩放图像测量出来等于或小于ImageView的边界范围,该图像将会完全显示
            .into(mPhotoView);
        return view;
    }
}